﻿using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Web;
using Hxsoft.AppV1.Module;

namespace Hxsoft.AppV1.WebUI
{
	public class PageBlockSpecialModule : ISpecialModuleParser
	{
		public string ParsePart(SpecialTemplateConfiguration templateConfiguration, SpecialModule module, SpecialPageConfiguration configuration, Special specialInfo, SpecialPage pageInfo, ref SpecialPageInfo page, bool isPreview)
		{
			string content = "";
			if (configuration == null) return content;
			if (module == null || module.ModuleId != configuration.ModuleId) return content;

			SpecialBlockPageConfiguration con = configuration as SpecialBlockPageConfiguration;
			if (con == null) con = new SpecialBlockPageConfiguration();
			SpecialContentTemplate contentTemplate = templateConfiguration.GetBlockTemplate(module.ModuleType, con.ListType);
			if (contentTemplate == null) return content;

			content = contentTemplate.GetTemplate(con.Parameters);
			Regex reg = new Regex(@"<headerTemplate>((?:.|\n|\r)*?)</headerTemplate>", RegexOptions.IgnoreCase);
			if (con.ShowHeader == false)
				content = reg.Replace(content, "");
			else
				content = reg.Replace(content, "$1");
			content = content.Replace("{ListTitle}", module.ModuleName);
			content = content.Replace("{CssClass}", string.IsNullOrEmpty(con.CssClass) ? "" : @" class=""" + con.CssClass + @"""");
			content = content.Replace("{AttachedClass}", string.IsNullOrEmpty(con.CssClass) ? "" : @" " + con.CssClass);
			content = content.Replace("{ModuleIndex}", page.ModuleCounter.ToString());

			Module.PageBlock block = null;

			if (isPreview)	// 读预览数据。
			{
				string json = templateConfiguration.GetBlockPreviewData(module.ModuleType, contentTemplate.ViewType);
				if (!string.IsNullOrEmpty(json)) block = Serialization.DeserializeJson<Module.PageBlock>(json);
			}

			if(block == null) block = PageBlocks.GetItem(int.Parse(module.RefModuleId));

			if (block != null)
				content = content.Replace("{Content}", block.Content);
			else
				content = content.Replace("{Content}", "");

			if (contentTemplate.HeadIncludeFiles != null)	// 头部包含文件
			{
				foreach (var file in contentTemplate.HeadIncludeFiles)
					page.RegisterHeadIncludeFile(file.Key, file.Value);
			}
			
			if (isPreview)		// 加载预览样式
			{
				page.TemplateCssUrls = templateConfiguration.TemplateCssUrls;
				if (page.TemplateCssUrls != null)
				{
					for (int i = 0; i < page.TemplateCssUrls.Length; i++)
					{
						var url = page.TemplateCssUrls[i];
						if (!string.IsNullOrEmpty(url))
							page.TemplateCssUrls[i] = url.Replace(SpecialPages.TemplateConst_TemplateRootUrl, SpecialPages.TemplateRootUrl);
					}
				}
			}

			// 替换其它模板变量
			content = GenericHelper.ReplaceSpecialTemplateParameters(content, contentTemplate.OtherParameters, con.Parameters);

			return content;
		}

		#region ISpecialModuleParser 成员

		string ISpecialModuleParser.ParsePreviewPart(SpecialTemplateConfiguration templateConfiguration, SpecialModule module, SpecialPageConfiguration configuration, Special specialInfo, SpecialPage pageInfo, ref SpecialPageInfo page)
		{
			return ParsePart(templateConfiguration, module, configuration, specialInfo, pageInfo, ref page, true);
		}

		string ISpecialModuleParser.ParsePreviewContent(SpecialTemplateConfiguration templateConfiguration, SpecialModule module, Special specialInfo, SpecialPage pageInfo, ref SpecialPageInfo page, string moduleView)
		{
			return "";
		}


		string ISpecialModuleParser.ParsePart(SpecialTemplateConfiguration templateConfiguration, SpecialModule module, SpecialPageConfiguration configuration, Special specialInfo, SpecialPage pageInfo, ref SpecialPageInfo page)
		{
			return ParsePart(templateConfiguration, module, configuration, specialInfo, pageInfo, ref page, false);
		}

		string ISpecialModuleParser.ParseContent(SpecialTemplateConfiguration templateConfiguration, SpecialModule module, Special specialInfo, SpecialPage pageInfo, ref SpecialPageInfo page)
		{
			return "";
		}

		#endregion
	}
}